<template>
  <div style="padding: 10px 15px">
    <!-- 查询条件-->
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="请输入单号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="form.area"
        placeholder="请输入区域代码"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="form.recdevice"
        placeholder="请输入收银机号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="form.cpnID"
        filterable
        clearable
        class="filter-item"
        placeholder="请选择企业"
        @change="CpnselectFn($event)"
        v-if="this.user.cpnId == sy"
      >
        <el-option
          v-for="item in cpnoptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>

      <el-select
        v-model="form.orgid"
        clearable
        class="filter-item"
        placeholder="请选择机构"
      >
        <el-option
          v-for="item in cpnorgoptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>

      <el-select
        v-model="form.payTyp"
        clearable
        class="filter-item"
        placeholder="请选择支付方式"
      >
        <el-option
          v-for="item in paytypeoptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>

      <el-select
        v-model="form.payChannel"
        clearable
        filterable
        class="filter-item"
        placeholder="请选择支付通道"
        v-if="this.user.cpnId == sy"
      >
        <el-option
          v-for="item in paytychanneloptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>

      <el-select
        v-model="Stt"
        class="filter-item"
        filterable
        clearable
        placeholder="请选择支付状态"
      >
        <el-option
          v-for="item in SttOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>

      <el-col :span="3" style="margin-right: 10px">
        <el-date-picker
          type="date"
          placeholder="请选择开始日期"
          v-model="form.StDt"
          style="width: 100%; margin-right: 10px"
          format="yyyy-MM-dd"
        ></el-date-picker>
      </el-col>
      <el-col :span="3" style="margin-right: 10px">
        <el-date-picker
          type="date"
          placeholder="请选择结束日期"
          v-model="form.EdDt"
          style="width: 100%; margin-right: 10px"
          format="yyyy-MM-dd"
        ></el-date-picker>
      </el-col>
      <el-button
        class="filter-item seachbttn"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
    </div>
    <div
      class="filter_fenge"
      style="border: none; border-top: 1px solid #d1ddeb"
    ></div>
    <div class="filter-container">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="exportFormVisible = true"
        >导出</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="errorFormVisible = true"
        >异常订单查询</el-button
      >
    </div>

    <!--查询条件 end-->

    <!--table-->
    <el-table
      v-loading="listLoading"
      :data="PayRecordHelpData"
      fit
      highlight-current-row
      style="width: 100%"
      stripe
    >
      <el-table-column
        label="企业名称"
        align="left"
        min-width="150px"
        v-if="this.logincpnid == sy"
      >
        <template slot-scope="{ row }">
          <span>{{ row.cpnidstr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店" align="left" min-width="180">
        <template slot-scope="{ row }">
          <span>[{{ row.orgid }}]{{ row.orgname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收银机号" align="left" min-width="100px">
        <template slot-scope="{ row }">
          <span>{{ row.recdevice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收银员" align="left" min-width="100px">
        <template slot-scope="{ row }">
          <span>{{ row.recman }}</span>
        </template>
      </el-table-column>
      <el-table-column label="区域" align="left" min-width="80px">
        <template slot-scope="{ row }">
          <span>{{ row.area }}</span>
        </template>
      </el-table-column>
      <el-table-column label="西联单号" align="left" min-width="280">
        <template slot-scope="{ row }">
          <span>{{ row.payid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="三方单号" align="left" min-width="280">
        <template slot-scope="{ row }">
          <span>{{ row.transid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="left" min-width="90px">
        <template slot-scope="{ row }">
          <span>{{ row.paytypstr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付通道" align="left" min-width="160px">
        <template slot-scope="{ row }">
          <span>{{ row.paychannelstr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额/元" align="right" min-width="120px">
        <template slot-scope="{ row }">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易状态" align="left" min-width="90px">
        <template slot-scope="{ row }">
          <span
            v-if="row.stt == 1 || row.stt == 2 || row.stt == 3 || row.stt == 5"
            :class="[
              row.stt == 1 || row.stt == 2 || row.stt == 3 || row.stt == 5
                ? 'green'
                : 'red',
            ]"
            >{{ row.sttstr }}</span
          >

          <span
            v-if="row.stt != 1 && row.stt != 2 && row.stt != 3 && row.stt != 5"
            class="red"
            title="点击查看当前订单异常错误明细以及消费日志记录"
            @click="ShowError(row)"
            ><a>查看异常</a></span
          >
        </template>
      </el-table-column>
      <el-table-column label="请求时间" align="left" min-width="170px">
        <template slot-scope="{ row }">
          <span>{{ row.reqdt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="left" min-width="170px">
        <template slot-scope="{ row }">
          <span>{{ row.resdt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易类型" align="left" min-width="90px">
        <template slot-scope="{ row }">
          <span>{{ row.typstr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款账户" align="left" min-width="230px">
        <template slot-scope="{ row }">
          <span>{{ row.pid }}</span>
        </template>
      </el-table-column>

      <el-table-column label="响应时间/毫秒" align="left" min-width="156px">
        <template slot-scope="{ row }">
          <span>{{ row.restimes }}</span>
        </template>
      </el-table-column>
     <!--
   <el-table-column label="买方账户" align="left" min-width="230px">
        <template slot-scope="{ row }">
          <span  v-if="row.stt == 1 || row.stt == 2 || row.stt == 3 || row.stt == 5" >{{ row.uid }}</span>
        </template>
      </el-table-column>

     -->

      <el-table-column label="退款金额/元" align="right" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.rfundamt }}</span>
        </template>
      </el-table-column>

      <el-table-column label="退款单号" align="left" min-width="350px">
        <template slot-scope="{ row }">
          <span>{{ row.rfundid }}</span>
        </template>
      </el-table-column>
      <el-table-column label="错误信息" align="left" min-width="400px">
        <template slot-scope="{ row }">
          <span v-if="row.stt != 1 && row.stt != 2 && row.stt != 3 && row.stt!= 5">{{ row.msg }}</span>
        </template>
      </el-table-column>
     
          <el-table-column label="商品信息" align="left" min-width="400px">
        <template slot-scope="{ row }">
          <span v-if="listQuery.title!=''&&listQuery.title!=null">{{ row.godsinfo.replace("\\","") }}</span>
        </template>
      </el-table-column>
      
    </el-table>

    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData(this)"
    />

    <!--table end -->

    <el-dialog title="导出" :visible.sync="exportFormVisible" width="20%">
      <el-form :model="downloadform" :inline="true" label-position="right">
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          :loading="downloadform.nowexportLoading"
          @click="Export(false)"
          >按当前页导出</el-button
        >
        <!--
            <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          :loading="downloadform.allexportLoading"
          @click="Export(true)"
        >全部导出</el-button>
        -->
      </el-form>
    </el-dialog>

    <el-dialog title="错误信息" :visible.sync="errorFormVisible" width="70%">
      <error_msg :error_msg="error_msg_from" :is-show="false" />
    </el-dialog>

    <!--弹出层 end-->
  </div>
</template>


<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { AjaxToken, TipConfirm } from "@/api/common";
import { SelectAjax } from "@/api/common";
import { Jurisdiction } from "@/api/common";
import sessionStorage from "@/api/Storage";
import "url-search-params-polyfill";
import commconfig from "@/comm.json";
import error_msg from "./components/error_msg"; // 企业结算信息
export default {
  name: "Itemized",
  components: { Pagination, error_msg },
  methods: {
    Export(Export) {
      if (Export) {
        this.downloadform.allexportLoading = true;
      } else {
        this.downloadform.nowexportLoading = true;
      }
      backstage.PayRecordHelpExport(this, Export);
    },
    ShowError(row) {
      this.error_msg_from.orderid = row.payid;
      this.errorFormVisible = true;
    },
    //权限
    Jurisdiction(moduleActionName) {
      return Jurisdiction(moduleActionName, this);
    },
    //多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //多选赋值
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    //搜索
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchData();
    },
    //新增
    handleCreate() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    //编辑
    handleClick(row) {
      this.dialogStatus = "edit";
      this.dialogFormVisible = true;
    },
    //保存
    saveForm() {
      var arr = this.multipleSelection;
      var params = new URLSearchParams();
      backstage.SavePayRecordHelp(this, params);
    },

    CpnselectFn(e) {
      this.cpnorgoptions = [];
      this.form.cpnID = e;

      backstage.GetCpnOrgList(this);
    },
    //删除
    deleteClick(row) {
      var multis = [];
      var params = new URLSearchParams();
      if (row == undefined || row == "") {
        var arr = this.multipleSelection;
        if (arr == undefined || arr.length <= 0) {
          this.$message({
            showClose: true,
            message: "请至少选中一行",
            type: "error",
          });
          this.listLoading = false;
          return;
        } else {
          for (var i = 0; i < arr.length; i++) {
            multis.push(arr[i].id);
          }
          params.append("delds", JSON.stringify(multis));
        }
      } else {
        multis.push(row.id);
        params.append("delds", JSON.stringify(multis));
      }
      this.$confirm("确认删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then((a) => {
          this.listLoading = true;
          backstage.DelPayRecordHelp(this, params);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.listLoading = false;
        });
    },
    //查询
    fetchData() {
      this.listLoading = true;
      var params = new URLSearchParams();
      params.append(
        "Parms",
        '{"page":' +
          this.listQuery.page +
          ',"rows":' +
          this.listQuery.limit +
          "}"
      );
      var user = JSON.parse(sessionStorage.getItem("userinfo"));
      params.append("Stt", this.Stt);

      params.append("OrgId", this.form.orgid);
      params.append("PyTypID", this.form.payTyp);
      params.append("PyChnlID", this.form.payChannel);
      params.append("Area", this.form.area);
      params.append("Rec_device", this.form.recdevice);
      

      

      params.append(
        "StDt",
        this.form.StDt == ""
          ? null
          : new Date(this.form.StDt).toLocaleDateString()
      );
      params.append(
        "EdDt",
        this.form.EdDt == ""
          ? null
          : new Date(this.form.EdDt).toLocaleDateString()
      );

      params.append("order_id", this.listQuery.title);
      params.append(
        "CpnID",
        this.logincpnid != "system" ? this.logincpnid : this.form.cpnID
      );

      backstage.GetPayRecordHelpLst(this, params);
    },
  },

  //初始化
  created() {
    var userinfo = JSON.parse(sessionStorage.getItem("userinfo"));
    this.user = userinfo;
    this.logincpnid = userinfo.cpnId;
    this.form.cpnID =
      userinfo.cpnId == "system" ? this.form.cpnID : userinfo.cpnId;
    backstage.GetEnum_PayStatuss(this);
    backstage.GeCpntListItems(this);
    backstage.GetPayTypeList(this);
    backstage.GetPyChannelSelectListItems(this);
    this.fetchData();
  },
  activated() {
    var userinfo = JSON.parse(sessionStorage.getItem("userinfo"));
    this.user = userinfo;
    this.logincpnid = userinfo.cpnId;
    backstage.GeCpntListItems(this);
    backstage.GetEnum_PayStatuss(this);
    this.fetchData();
  },
  data() {
    return {
      error_msg_from: {
        orderid: "",
      },
      Stt: "",
      user: null,
      SttOptions: [],
      sy: "system",
      logincpnid: "",
      cpnoptions: [],
      multipleSelection: [],
      formLabelWidth: "120px",
      PayRecordHelpData: null,
      paytypeoptions: [],
      paytychanneloptions: [],
      cpnorgoptions: [],
      listQuery: {
        title: "",
        total: 0,
        page: 1,
        limit: 10,
      },
      form: {
        StDt: "",
        EdDt: "",
        cpnID: "",
        payTyp: "",
        payChannel: "",
        orgid: "",
        area:"",
        recdevice:""
      },
      exportFormVisible: false,
      errorFormVisible: false,
      downloadform: {
        allexportLoading: false,
        nowexportLoading: false,
      },
      listLoading: true,
      dialogFormVisible: false,
      dialogdetailsVisible: false,
      dialogStatus: "",
      textMap: {
        update: "修改",
        create: "添加",
      },
    };
  },
};
//后台事件
var backstage = {
  PayRecordHelpExport: function (thisobj, Export) {
    var PayID = thisobj.listQuery.title;
    var CpnID =
      thisobj.logincpnid == "system" ? thisobj.form.cpnID : thisobj.logincpnid;
    var Stt = thisobj.Stt;
    var url =
      commconfig.baseapi +
      "PayRecordHelpExport?Parms=" +
      '{"page": "' +
      thisobj.listQuery.page +
      '","rows": "' +
      thisobj.listQuery.limit +
      '"}' +
      "&Export=" +
      Export +
      "&Stt=" +
      Stt +
      "&PayID=" +
      PayID +
      "&CpnID=" +
      CpnID +
      "&PyChnlID=" +
      thisobj.form.payChannel +
      "&OrgId=" +
      thisobj.form.orgid;
    if (Export) {
      thisobj.downloadform.allexportLoading = false;
    } else {
      thisobj.downloadform.nowexportLoading = false;
    }
    window.location.href = url;
  },
  //获取PayRecordHelp集合
  GetPayRecordHelpLst: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "GetPayRecordHelpLst",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data.data;
        thisobj.PayRecordHelpData = body.rows;
        thisobj.listQuery.page = body.page;
        thisobj.listQuery.total = body.records;
      },
      function (err) {}
    );
  },
  GetCpnOrgList: function (thisobj) {
    SelectAjax(
      "GetCpnOrgList?cpnid=" + thisobj.form.cpnID + "",
      function (response) {
        var body = response.data;
        thisobj.cpnorgoptions = [];
        body.data.forEach((element) => {
          thisobj.cpnorgoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GeCpntListItems: function (thisobj) {
    SelectAjax(
      "GeCpntListItems?val=" + thisobj.cpnid + "",
      function (response) {
        var body = response.data;
        thisobj.cpnoptions = [];
        body.data.forEach((element) => {
          thisobj.cpnoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  SavePayRecordHelp: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "SavePayRecordHelp",
      params,
      "post",
      function (res) {
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 2000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
  DelPayRecordHelp: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "DelPayRecordHelp",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 2000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },

  GetPayTypeList: function (thisobj) {
    SelectAjax(
      "GetPayTypeList?val=",
      function (response) {
        var body = response.data;
        thisobj.paytypeoptions = [];
        body.data.forEach((element) => {
          thisobj.paytypeoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetPyChannelSelectListItems: function (thisobj) {
    SelectAjax(
      "GetPyChannelSelectListItems?val=",
      function (response) {
        var body = response.data;
        thisobj.paytychanneloptions = [];
        body.data.forEach((element) => {
          thisobj.paytychanneloptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetEnum_PayStatuss: function (thisobj) {
    SelectAjax(
      "GetEnum_PayStatuss?isEmpty=" + true + "+&code=0",
      function (response) {
        var body = response.data;
        thisobj.SttOptions = [];
        body.data.forEach((element) => {
          thisobj.SttOptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
};
</script>